/**
 * Created by PhpStorm.
 * User: wangkk
 * Date: 2019-03-15 10:02
 * Description: {创建卡券的相关js}
 */
!function(){
    function init(){
        var that = this;
        //当前是否到期集合
        this.Ids = {
            cardTypeId : $("#couponform-card_type"),
            dateTypeId : $('#couponform-date_type'),
        };

        //卡券类型切换
        $(this.Ids.cardTypeId).on('change', function(){
            var cardType = $(this).val();
            switch (cardType) {
                case 'DISCOUNT': //折扣券
                    that.selectDiscountCard();
                    break;
                case 'GROUPON': // 团购券
                    that.selectGrouponCard();
                    break;
                case 'CASH':    //代金券
                    that.selectCashCard();
                    break;
                case 'GENERAL_COUPON': //优惠券
                    that.selectGeneralCouponCard();
                    break;
                case 'GIFT':   //兑换券
                    that.selectGiftCard();
                    break;
                default:
                    //隐藏其他字段
                    that.selectNoCard();
            }
        });
        //折扣券的相关显示
        this.selectDiscountCard = function(){
            //修改input属性
            $('#couponform-discount').prop('type','text');
            var discount_label = '<label class="control-label" for="couponform-discount">打折额度（30代表7折）</label>';
            //增加label
            $('#couponform-discount').before(discount_label);
            //隐藏其他的字段
            $('#couponform-deal_detail').attr('type', 'hidden');
            $('#couponform-least_cost').attr('type', 'hidden');
            $('#couponform-reduce_cost').attr('type', 'hidden');
            $('#couponform-gift').attr('type', 'hidden');
            $('#couponform-default_detail').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-deal_detail"]').remove();
            $('label[for="couponform-least_cost"]').remove();
            $('label[for="couponform-reduce_cost"]').remove();
            $('label[for="couponform-gift"]').remove();
            $('label[for="couponform-default_detail"]').remove();
        }
        //显示团购券的相关内容
        this.selectGrouponCard = function() {
            //修改input属性
            $('#couponform-deal_detail').prop('type','text');
            var deal_detail_label = '<label class="control-label" for="couponform-deal_detail">团购详情描述</label>';
            //增加label
            $('#couponform-deal_detail').before(deal_detail_label);
            //隐藏其他的字段
            $('#couponform-discount').attr('type', 'hidden');
            $('#couponform-least_cost').attr('type', 'hidden');
            $('#couponform-reduce_cost').attr('type', 'hidden');
            $('#couponform-gift').attr('type', 'hidden');
            $('#couponform-default_detail').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-discount"]').remove();
            $('label[for="couponform-least_cost"]').remove();
            $('label[for="couponform-reduce_cost"]').remove();
            $('label[for="couponform-gift"]').remove();
            $('label[for="couponform-default_detail"]').remove();
        }
        
        //优惠券的相关显示
        this.selectGeneralCouponCard = function () {
            //修改input属性
            $('#couponform-default_detail').attr('type', 'text');
            var default_detail_label = '<label class="control-label" for="couponform-default_detail">优惠券详情</label>';
            $('#couponform-default_detail').before(default_detail_label);
            //隐藏其他的字段
            $('#couponform-deal_detail').attr('type', 'hidden');
            $('#couponform-least_cost').attr('type', 'hidden');
            $('#couponform-reduce_cost').attr('type', 'hidden');
            $('#couponform-gift').attr('type', 'hidden');
            $('#couponform-discount').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-deal_detail"]').remove();
            $('label[for="couponform-least_cost"]').remove();
            $('label[for="couponform-reduce_cost"]').remove();
            $('label[for="couponform-gift"]').remove();
            $('label[for="couponform-discount"]').remove();
        }
        //代金券的相关字段显示
        this.selectCashCard = function () {
            //修改input属性
            $('#couponform-least_cost').attr('type', 'text');
            $('#couponform-reduce_cost').attr('type', 'text');
            var least_cost_label = '<label class="control-label" for="couponform-least_cost">起用金额（单位：分）</label>';
            var reduce_cost_label = '<label class="control-label" for="couponform-least_cost">减免金额（单位：分）</label>';
            $('#couponform-least_cost').before(least_cost_label);
            $('#couponform-reduce_cost').before(reduce_cost_label);
            //隐藏其他的字段
            $('#couponform-deal_detail').attr('type', 'hidden');
            $('#couponform-default_detail').attr('type', 'hidden');
            $('#couponform-gift').attr('type', 'hidden');
            $('#couponform-discount').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-deal_detail"]').remove();
            $('label[for="couponform-gift"]').remove();
            $('label[for="couponform-discount"]').remove();
            $('label[for="couponform-default_detail"]').remove();
        }
        //兑换券的相关显示内容
        this.selectGiftCard = function () {
            //修改input属性
            $('#couponform-gift').attr('type', 'text');
            var gift_label = '<label class="control-label" for="couponform-gift">兑换内容名称</label>';
            $('#couponform-gift').before(gift_label);
            //隐藏其他的字段
            $('#couponform-deal_detail').attr('type', 'hidden');
            $('#couponform-least_cost').attr('type', 'hidden');
            $('#couponform-reduce_cost').attr('type', 'hidden');
            $('#couponform-default_detail').attr('type', 'hidden');
            $('#couponform-discount').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-deal_detail"]').remove();
            $('label[for="couponform-least_cost"]').remove();
            $('label[for="couponform-reduce_cost"]').remove();
            $('label[for="couponform-default_detail"]').remove();
            $('label[for="couponform-discount"]').remove();
        }
        //未选择卡券类型
        this.selectNoCard = function () {
            //隐藏其他的字段
            $('#couponform-deal_detail').attr('type', 'hidden');
            $('#couponform-least_cost').attr('type', 'hidden');
            $('#couponform-reduce_cost').attr('type', 'hidden');
            $('#couponform-default_detail').attr('type', 'hidden');
            $('#couponform-discount').attr('type', 'hidden');
            $('#couponform-gift').attr('type', 'hidden');
            //移除其他的label
            $('label[for="couponform-deal_detail"]').remove();
            $('label[for="couponform-least_cost"]').remove();
            $('label[for="couponform-reduce_cost"]').remove();
            $('label[for="couponform-default_detail"]').remove();
            $('label[for="couponform-discount"]').remove();
            $('label[for="couponform-gift"]').remove();
        }
        //卡券使用时间类型切换
        $(this.Ids.dateTypeId).on('change', function () {
            var dateType = $(this).val();
            if(dateType == 'DATE_TYPE_FIX_TERM'){ //固定时长，自领取后按天计算
                that.select_date_type_fix_term();
            }else if(dateType == 'DATE_TYPE_FIX_TIME_RANGE'){ //固定的日期区间
                that.select_date_type_fix_time_range();
            }else{
                that.select_no_date_type();
            }
        });
        //选择使用日期类型
        this.select_date_type_fix_term = function () {
            //隐藏其他固定日期的字段
            $('label[for="couponform-begin_timestamp"]').remove();
            $('#couponform-begin_timestamp').attr('type', 'hidden');
            $('label[for="couponform-end_timestamp"]').remove();
            $('#couponform-end_timestamp').attr('type', 'hidden');
            //显示固定时长的字段
            $('#couponform-fixed_term').attr('type', 'text');
            $('#couponform-fixed_term').before('<label class="control-label" for="couponform-fixed_term">自领取后多少天内有效</label>');
            $('#couponform-fixed_begin_term').attr('type', 'text');
            $('#couponform-fixed_begin_term').before('<label class="control-label" for="couponform-fixed_begin_term">自领取后多少天后开始生效</label>');
            $('#couponform-end_timestamp').attr('type', 'text');
            $('#couponform-end_timestamp').before('<label class="control-label" for="couponform-end_timestamp">卡券过期时间</label>');
        }
        //选择使用日期类型
        this.select_date_type_fix_time_range = function () {
            //隐藏其他固定日期的字段
            $('label[for="couponform-fixed_term"]').remove();
            $('#couponform-fixed_term').attr('type', 'hidden');
            $('label[for="couponform-fixed_begin_term"]').remove();
            $('#couponform-fixed_begin_term').attr('type', 'hidden');
            $('label[for="couponform-end_timestamp"]').remove();
            $('#couponform-end_timestamp').attr('type', 'hidden');
            //显示固定时长的字段
            $('#couponform-begin_timestamp').attr('type', 'text');
            $('#couponform-begin_timestamp').before('<label class="control-label" for="couponform-begin_timestamp">卡券起用时间</label>');
            $('#couponform-end_timestamp').attr('type', 'text');
            $('#couponform-end_timestamp').before('<label class="control-label" for="couponform-end_timestamp">卡券过期时间</label>');
        }
        //未选择使用日期类型
        this.select_no_date_type = function(){
            //全部隐藏
            $('label[for="couponform-fixed_term"]').remove();
            $('#couponform-fixed_term').attr('type', 'hidden');
            $('label[for="couponform-fixed_begin_term"]').remove();
            $('#couponform-fixed_begin_term').attr('type', 'hidden');
            $('label[for="couponform-begin_timestamp"]').remove();
            $('#couponform-begin_timestamp').attr('type', 'hidden');
            $('label[for="couponform-end_timestamp"]').remove();
            $('#couponform-end_timestamp').attr('type', 'hidden');
        }
        //初始化
        $(function(){
            var cardType = $('#couponform-card_type').val();
            switch (cardType) {
                case 'DISCOUNT': //折扣券
                    that.selectDiscountCard();
                    break;
                case 'GROUPON': // 团购券
                    that.selectGrouponCard();
                    break;
                case 'CASH':    //代金券
                    that.selectCashCard();
                    break;
                case 'GENERAL_COUPON': //优惠券
                    that.selectGeneralCouponCard();
                    break;
                case 'GIFT':   //兑换券
                    that.selectGiftCard();
                    break;
                default:
                    //隐藏其他字段
                    that.selectNoCard();
            }
            var dateType = $('#couponform-date_type').val();
            if(dateType == 'DATE_TYPE_FIX_TERM'){ //固定时长，自领取后按天计算
                that.select_date_type_fix_term();
            }else if(dateType == 'DATE_TYPE_FIX_TIME_RANGE'){ //固定的日期区间
                that.select_date_type_fix_time_range();
            }else{
                that.select_no_date_type();
            }
        });
    }
    window.createCard = init;
}();
//使用
var createCard = new createCard();

$(function () {
    $('#couponform-begin_timestamp').prop('readonly', true);
    $('#couponform-end_timestamp').prop('readonly', true);
    //设置语言
    setdatetimepickerlang();
    //使用插件
    $('#couponform-begin_timestamp').datetimepicker({
        language:  'zh',
        format : 'yyyy-mm-dd',
        minView : 2,
        autoclose :  1,
        weekStart : 1,
        //startDate : getCurTime(),
        todayHighlight : true,
    });
    $('#couponform-end_timestamp').datetimepicker({
        language:  'zh',
        format : 'yyyy-mm-dd',
        minView : 2,
        autoclose :  1,
        weekStart : 1,
        //startDate : getCurTime(),
        todayHighlight : true,
    });
});
//设置语言
function setdatetimepickerlang(){
    $('#couponform-begin_timestamp').datetimepicker.dates['zh'] = {
        days:       ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日"],
        daysShort:  ["日", "一", "二", "三", "四", "五", "六","日"],
        daysMin:    ["日", "一", "二", "三", "四", "五", "六","日"],
        months:     ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月","十二月"],
        monthsShort:  ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
        meridiem:    ["上午", "下午"],
        today:       "今天"
    };
    $('#couponform-end_timestamp').datetimepicker.dates['zh'] = {
        days:       ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日"],
        daysShort:  ["日", "一", "二", "三", "四", "五", "六","日"],
        daysMin:    ["日", "一", "二", "三", "四", "五", "六","日"],
        months:     ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月","十二月"],
        monthsShort:  ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
        meridiem:    ["上午", "下午"],
        today:       "今天"
    };
}
//获取今天日期
function getCurTime(){
    var dateObj = new Date();
    var year = dateObj.getFullYear();//获取当前年
    var yue = dateObj.getMonth()+1;//获取当前月
    var date = dateObj.getDate();//获取当前日
    return year+'-'+yue+'-'+date;   // 2019-02-22 10
}
//选择适用门店
$('#select-stores').on('click', function () {
    layer.open({
        type: 2,
        title: '选择适用门店（不选表示适用所有门店）',
        shadeClose: true,
        shade: false,
        maxmin: true, //开启最大化最小化按钮
        area: ['800px', '550px'],
        content: 'http://www.wxcard.com/coupon/getapplystores'
    });
});
//得到适用门店，赋值
function setApplyStores(storeIds){
    $('#couponform-apply_stores').val(storeIds);
    console.log(storeIds);
    $('#storesNum').remove();
    if(storeIds.length > 0){
        var length = storeIds.length;
        $('#select-stores').after('<span id="storesNum">选择了'+length+'个门店</span>');
    }else{
        $('#select-stores').after('<span id="storesNum">选择了0个门店，表示全部适用</span>');
    }
}
//选择适用商品
$('#select-goods').on('click', function () {
    layer.open({
        type: 2,
        title: '选择适用商品（不选表示适用所有商品）',
        shadeClose: true,
        shade: false,
        maxmin: true, //开启最大化最小化按钮
        area: ['800px', '550px'],
        content: 'http://www.wxcard.com/coupon/getapplygoods'
    });
});
//得到适用商品，赋值
function setApplygoods(goodsIds){
    $('#couponform-apply_goods').val(goodsIds);
    console.log(goodsIds);
    $('#goodsNum').remove();
    if(goodsIds.length > 0){
        var length = goodsIds.length;
        $('#select-goods').after('<span id="goodsNum">选择了'+length+'个商品</span>');
    }else{
        $('#select-goods').after('<span id="goodsNum">选择了0个商品，表示全部适用</span>');
    }
}